##################################################################
# Clean qualtrics data
##################################################################
#set working directory
setwd("~/Dropbox/Gender and Judicial Elections/JOP replication files")
#clear working environment
rm(list=ls())
#load packages
library(tidyverse) 
library(scales)
library(ggplot2)
library(stargazer)
library(readstata13)
library(splitstackshape)
library(lubridate)
library(lfe)
library(ggpubr)
library(xtable)
library(MASS)
library(reshape2)
library(openxlsx)
library(vtable)

########################################################################
# Ads Analysis
########################################################################
ads <- read.xlsx("./WMP_data.xlsx") %>% mutate(creative=str_trim(creative)) %>%
  full_join(.,read.xlsx("./WMP_coding.xlsx") %>%
              mutate(creative=str_trim(creative))) %>%
  mutate(across(starts_with("trait_code"),
                ~dplyr::recode(., `1`="bold",`2`="caring",`3`="compassionate",`4`="competent",
                               `5`="experienced",`6`="fatherlymotherly",`8`="fighter",`9`="hardworker",
                               `10`="honesty",`11`="independent",`12`="innovative",`13`="protect",
                               `14`="proven",`15`="selfmade",`16`="tough",`17`="courage",`18`="stoodupto",
                               `19`="trust",`20`="leader",`21`="careerpol",`22`="corrupt",
                               `23`="dangerous",`24`="dishonest",`25`="hypocrite",`26`="incompetent",
                               `27`="negative",`28`="reckless",`29`="softweak",`31`="insider",`32`="strong",
                               `33`="compromising",`34`="reform",`35`="aggressive",`36`="justice",
                               `37`="fairness",`38`="secondchance",`39`="victim",`40`="safe",
                               `41`="othercourts",`42`="policeendorse",`43`="prosecutor")
  )) %>%
  unite("trait", c(trait_code1:trait_code8,trait_code9), na.rm = TRUE, remove = FALSE,sep=",") %>%
  splitstackshape::cSplit_e(., "trait", sep = ',', fill = 0, type = "character") %>%
  full_join(.,read.xlsx("./candidate_level_clean.xlsx") %>%
              dplyr::select(-category),by="cand_id") %>%
  group_by(cand_id) %>%
  fill(gender_char, .direction = "downup") %>% ungroup() %>% distinct() %>%
  mutate(toughtotal=trait_tough+trait_fighter+trait_prosecutor+trait_policeendorse+trait_experienced+trait_protect+trait_strong,
         anytough = ifelse(toughtotal!=0,1,0),
         reformtotal=trait_reform+trait_compassionate+trait_victim+trait_secondchance,
         anyreform = ifelse(reformtotal!=0,1,0),
         tone=ifelse(tone_num==0,NA,ifelse(tone_num==1,"contrast",ifelse(tone_num==2,"negative","positive"))),
         multiple_winners=ifelse(is.na(multiple_winners),0,1),
         vote_per=vote_per*100,gender=ifelse(gender_char=="female",1,0)) 

ads_all <- ads %>%
  left_join(.,read.xlsx("./WMP_allairingsdata.xlsx") %>%
              mutate(airdate=openxlsx::convertToDate(airdate)),
            by=c("creative"="creative")) %>%
  group_by(creative) %>% fill(everything(),.direction = "downup") %>% distinct() %>%
  mutate(no.ads=n(),month=month(as.Date(airdate,"%Y/%M/%D")),
         toughtotal=trait_tough+trait_fighter+trait_prosecutor+trait_policeendorse+trait_experienced,
         anytough = ifelse(toughtotal!=0,1,0),
         reformtotal=trait_reform+trait_compassionate+trait_victim+trait_secondchance,
         anyreform = ifelse(reformtotal!=0,1,0),tone=tolower(tone))

#######################################
# Table 1
#######################################
summary <- ads %>% dplyr::select(gender,est_cost,demmention,repmention,won,number_candidates,
                                 multiple_winners,vote_per,incumbent,former_prosecutor,
                                 trait_tough,trait_fighter,trait_prosecutor,
                                 trait_policeendorse,trait_experienced,trait_protect,trait_strong,trait_reform,
                                 trait_compassionate,trait_victim,trait_secondchance,
                                 toughtotal,anytough,reformtotal,anyreform) %>% as.data.frame() %>%
  dplyr::mutate(gender=as.numeric(gender))
stargazer(summary,covariate.labels = c("Female Candidate","Logged Ad Cost","Democrat Mention","Republican Mention","Candidate Won","Number of Candidates","Multiple Winners","Vote Percentage","Incumbent","Former Prosecutor",
                                       "Tough","Fight","Prosecutor",
                                       "Police Endorse","Experienced","Protect","Strong","Reform","Compassionate","Victim","Second Chance",
                                       "Tough Index (1-7)","Any Tough","Reform Index (1-4)","Any Reform"),label="summarystatsads",digits=3,
          title="Summary Statistics of Unique Judicial Ads in 2017 and 2018",
          out="./tables/TableA1.tex")

#######################################
# Table 2
#######################################
r1a <- felm(trait_tough~as.factor(gender)|0|0|0,data=ads)
r2a <- felm(trait_fighter~as.factor(gender)|0|0|0,data=ads)
r3a <- felm(trait_prosecutor~as.factor(gender)|0|0|0,data=ads)
r4a <- felm(trait_policeendorse~as.factor(gender)|0|0|0,data=ads)
r5a <- felm(trait_experienced~as.factor(gender)|0|0|0,data=ads)
r6a <- felm(trait_protect~as.factor(gender)|0|0|0,data=ads)
r7a <- felm(trait_strong~as.factor(gender)|0|0|0,data=ads)

stargazer(r1a,r2a,r3a,r4a,r5a,r6a,r7a,title="Gender and Tough-on-Crime Campaign Appeals in 2017 and 2018 Judicial Elections, Unique Ads Only (Bivariate Results)",
          notes="Results from an OLS regression.",
          covariate.labels = "Female",
          dep.var.labels = c("Tough","Fight","Prosecutor","Police Endorse","Experienced","Tough","Fight","Prosecutor","Police Endorse","Experienced","Protect","Strong"),label = "toughappealsfull",
          out = "./tables/TableA2.tex",notes.align = "l",font.size="tiny",column.sep.width = "-10pt")

#######################################
# Table 3
#######################################
r1a <- felm(trait_reform~as.factor(gender)|0|0|0,data=ads)
r2a <- felm(trait_compassionate~as.factor(gender)|0|0|0,data=ads)
r3a <- felm(trait_victim~as.factor(gender)|0|0|0,data=ads)
r4a <- felm(trait_secondchance~as.factor(gender)|0|0|0,data=ads)

stargazer(r1a,r2a,r3a,r4a,title="Gender and Reform Campaign Appeals in 2017 and 2018 Judicial Elections, Unique Ads Only (Bivariate Results)",
          notes="Results from an OLS regression. ",
          covariate.labels = "Female",
          dep.var.labels = c("Reform","Compassionate","Victim","Second Chance","Reform","Compassionate","Victim","Second Chance"),label = "rehabappealsfull",
          out = "./tables/TableA3.tex",notes.align = "l",font.size="tiny",column.sep.width = "-7pt") 

#######################################
# Table 4
#######################################
partisan <- ads %>% filter(election_type=="partisan_election")

r1a <- felm(trait_tough~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category|0|0,data=partisan)
r2a <- felm(trait_fighter~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category|0|0,data=partisan)
r3a <- felm(trait_prosecutor~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category|0|0,data=partisan)
r4a <- felm(trait_policeendorse~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category|0|0,data=partisan)
r5a <- felm(trait_experienced~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category|0|0,data=partisan)
r6a <- felm(trait_protect~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category|0|0,data=partisan)
r7a <- felm(trait_strong~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category|0|0,data=partisan)

stargazer(r1a,r2a,r3a,r4a,r5a,r6a,r7a,title="Gender and Tough-on-Crime Campaign Appeals in 2017 and 2018 Judicial Elections, Unique Ads Only (Partisan Elections)",
          notes=c("Results from an OLS regression. Ad tone, program type, part of day,","affiliate, and race category fixed effects included."),
          covariate.labels = c("Female","Logged Ad Cost","Democrat Mention","Republican Mention","Candidate Won","Number of Candidates","Multiple Winners","Vote Percentage","Incumbent","Former Prosecutor"),
          dep.var.labels = c("Tough","Fight","Prosecutor","Police Endorse","Experienced","Protect","Strong","Tough","Fight","Prosecutor","Police Endorse","Experienced","Protect","Strong"),label = "toughappealspartisan",
          out = "./tables/TableA4.tex",notes.align = "l",font.size="tiny",column.sep.width = "-10pt")

#######################################
# Table 5
#######################################
nonpartisan <- ads %>% filter(election_type=="nonpartisan_election")

r1 <- felm(trait_tough~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category|0|0,data=nonpartisan)
r2 <- felm(trait_fighter~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category|0|0,data=nonpartisan)
r3 <- felm(trait_prosecutor~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category|0|0,data=nonpartisan)
r4 <- felm(trait_policeendorse~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category|0|0,data=nonpartisan)
r5 <- felm(trait_experienced~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category|0|0,data=nonpartisan)
r6 <- felm(trait_protect~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category|0|0,data=nonpartisan)
r7 <- felm(trait_strong~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category|0|0,data=nonpartisan)

stargazer(r1,r2,r3,r4,r5,r6,r7,title="Gender and Tough-on-Crime Campaign Appeals in 2017 and 2018 Judicial Elections, Unique Ads Only (Nonpartisan Elections)",
          notes=c("Results from an OLS regression. Ad tone, program type, part of day,","affiliate, and race category fixed effects included."),
          covariate.labels = c("Female","Logged Ad Cost","Democrat Mention","Republican Mention","Candidate Won","Number of Candidates","Multiple Winners","Vote Percentage","Incumbent","Former Prosecutor"),
          dep.var.labels = c("Tough","Fight","Prosecutor","Police Endorse","Experienced","Protect","Strong","Tough","Fight","Prosecutor","Police Endorse","Experienced","Protect","Strong"),label = "toughappealsnonpartisan",
          out = "./tables/TableA5.tex",notes.align = "l",font.size="tiny",column.sep.width = "-10pt")

#######################################
# Table 6
#######################################
r1 <- felm(trait_reform~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category|0|0,data=nonpartisan)
r2 <- felm(trait_compassionate~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category|0|0,data=nonpartisan)
r3 <- felm(trait_victim~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category|0|0,data=nonpartisan)
r4 <- felm(trait_secondchance~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category|0|0,data=nonpartisan)
r1a <- felm(trait_reform~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category|0|0,data=partisan)
r2a <- felm(trait_compassionate~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category|0|0,data=partisan)
r3a <- felm(trait_victim~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category|0|0,data=partisan)
r4a <- felm(trait_secondchance~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category|0|0,data=partisan)

stargazer(r1a,r2a,r3a,r4a,r1,r2,r3,r4,title="Gender and Reform Campaign Appeals in 2017 and 2018 Judicial Elections, Unique Ads Only (By Election Type)",
          notes=c("Results from an OLS regression. Ad tone, program type, part of day,","affiliate, and race category fixed effects included."),
          covariate.labels = c("Female","Logged Ad Cost","Democrat Mention","Republican Mention","Candidate Won","Number of Candidates","Multiple Winners","Vote Percentage","Incumbent","Former Prosecutor"),
          dep.var.labels = c("Reform","Compassionate","Victim","Second Chance","Reform","Compassionate","Victim","Second Chance"),label = "rehabappealselectiontype",
          out = "./tables/TableA6.tex",notes.align = "l",font.size="tiny",column.sep.width = "-7pt",
          column.labels = c("Nonpartisan","Nonpartisan","Nonpartisan","Nonpartisan","Partisan","Partisan","Partisan","Partisan")) 

#######################################
# Table 7
#######################################
r1 <- felm(trait_tough~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|0,data=ads)
r2 <- felm(trait_fighter~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|0,data=ads)
r3 <- felm(trait_prosecutor~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|0,data=ads)
r4 <- felm(trait_policeendorse~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|0,data=ads)
r5 <- felm(trait_experienced~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|0,data=ads)
r6 <- felm(trait_protect~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|0,data=ads)
r7 <- felm(trait_strong~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|0,data=ads)

alpha <- cbind(getfe(r1),getfe(r2),getfe(r3),getfe(r4),getfe(r5),getfe(r6),getfe(r7)) %>%
  .[, c(4,5,1,6,11,16,21,26,31)] %>%
  dplyr::rename(`Fixed Effects`=fe,`Fixed Effect Level`=idx,
                `Column 1`=effect,`Column 2`=effect.1,
                `Column 3`=effect.2, `Column 4`=effect.3,
                `Column 5`=effect.4,`Column 6`=effect.5,`Column 7`=effect.6) 

print(xtable(alpha,label="alpha_tough",caption="Fixed effects results from Table 2."), include.rownames=FALSE,
      file="./tables/TableA7.tex", tabular.environment = "longtable",size="\\tiny") 

#######################################
# Table 8
#######################################
r1 <- felm(trait_reform~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|0,data=ads)
r2 <- felm(trait_compassionate~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|0,data=ads)
r3 <- felm(trait_victim~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|0,data=ads)
r4 <- felm(trait_secondchance~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|0,data=ads)

alpha <- cbind(getfe(r1),getfe(r2),getfe(r3),getfe(r4)) %>%
  .[, c(4,5,1,6,11,16)] %>%
  dplyr::rename(`Fixed Effects`=fe,`Fixed Effect Level`=idx,
                `Column 1`=effect,`Column 2`=effect.1,
                `Column 3`=effect.2, `Column 4`=effect.3) 

print(xtable(alpha,label="alpha_reform",caption="Fixed effects results from Table 3."), include.rownames=FALSE,
      file="./tables/TableA8.tex",size="\\tiny") 

#######################################
# Table 9
#######################################
r1 <- felm(trait_tough~as.factor(gender)+log(no.ads)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|0,data=ads_all)
r2 <- felm(trait_fighter~as.factor(gender)+log(no.ads)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|0,data=ads_all)
r3 <- felm(trait_prosecutor~as.factor(gender)+log(no.ads)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|0,data=ads_all)
r4 <- felm(trait_policeendorse~as.factor(gender)+log(no.ads)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|0,data=ads_all)
r5 <- felm(trait_experienced~as.factor(gender)+log(no.ads)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|0,data=ads_all)
r6 <- felm(trait_protect~as.factor(gender)+log(no.ads)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|0,data=ads_all)
r7 <- felm(trait_strong~as.factor(gender)+log(no.ads)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|0,data=ads_all)

stargazer(r1,r2,r3,r4,r5,r6,r7,title="Gender and Tough-on-Crime Campaign Appeals in 2017 and 2018 Judicial Elections, All Ad Airings Included",
          notes=c("Results from an OLS regression. Ad tone, program type, part of day, state,","affiliate, race category, and election type fixed effects included."),
          covariate.labels = c("Female","Logged Number of Ads","Logged Ad Cost","Democrat Mention","Republican Mention","Candidate Won","Number of Candidates","Multiple Winners","Vote Percentage","Incumbent","Former Prosecutor"),
          dep.var.labels = c("Tough","Fight","Prosecutor","Police Endorse","Experienced","Protect","Strong"),label = "toughappeals",
          out = "./tables/TableA9.tex",notes.align = "l",font.size = "tiny",column.sep.width = "-10pt")

#######################################
# Table 10
#######################################
r1 <- felm(trait_reform~as.factor(gender)+log(no.ads)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|0,data=ads_all)
r2 <- felm(trait_compassionate~as.factor(gender)+log(no.ads)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|0,data=ads_all)
r3 <- felm(trait_victim~as.factor(gender)+log(no.ads)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|0,data=ads_all)
r4 <- felm(trait_secondchance~as.factor(gender)+log(no.ads)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|0,data=ads_all)

stargazer(r1,r2,r3,r4,title="Gender and Reform Campaign Appeals in 2017 and 2018 Judicial Elections, All Ad Airings Included",
          notes=c("Results from an OLS regression. Ad tone, program type, part of day, state,","affiliate, race category, and election type fixed effects included."),
          covariate.labels = c("Female","Logged Number of Ads","Logged Ad Cost","Democrat Mention","Republican Mention","Candidate Won","Number of Candidates","Multiple Winners","Vote Percentage","Incumbent","Former Prosecutor"),
          dep.var.labels = c("Reform","Compassionate","Victim","Second Chance","Reform","Compassionate","Victim","Second Chance"),label = "rehabappeals",
          out = "./tables/TableA10.tex",notes.align = "l",font.size = "tiny",column.sep.width = "-7pt") 

#######################################
# Table 11
#######################################
r1logit <- glm(trait_tough~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor,data=ads, family = "binomial")
r2logit <- glm(trait_fighter~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor,data=ads, family = "binomial")
r3logit <- glm(trait_prosecutor~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor,data=ads, family = "binomial")
r4logit <- glm(trait_policeendorse~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor,data=ads, family = "binomial")
r5logit <- glm(trait_experienced~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor,data=ads, family = "binomial")
r6logit <- glm(trait_protect~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor,data=ads, family = "binomial")
r7logit <- glm(trait_strong~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor,data=ads, family = "binomial")

OR <- function(x) exp(x)
stargazer(r1logit,r2logit,r3logit,r4logit,r5logit,r6logit,r7logit,
          apply.coef = OR, keep = c('gender','est_cost','demmention','repmention','won','number_candidates','multiple_winners','vote_per','incumbent','former_prosecutor'),title="Gender and Tough-on-Crime Campaign Appeals in 2017 and 2018 Judicial Elections, Unique Ads Only (Logit Odds Ratios)",
          notes="Results from logit regressions. Values represent odds ratios.",
          covariate.labels = c("Female","Logged Ad Cost","Democrat Mention","Republican Mention","Candidate Won","Number of Candidates","Multiple Winners","Vote Percentage","Incumbent","Former Prosecutor"),
          dep.var.labels = c("Tough","Fight","Prosecutor","Police Endorse","Experienced","Protect","Strong"),label = "toughappealslogit",
          out = "./tables/TableA11.tex",notes.align = "l",font.size="tiny",column.sep.width = "-10pt")

#######################################
# Table 12
#######################################
r1logit <- glm(trait_reform~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor,data=ads, family = "binomial")
r2logit <- glm(trait_compassionate~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor,data=ads, family = "binomial")
r3logit <- glm(trait_victim~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor,data=ads, family = "binomial")
r4logit <- glm(trait_secondchance~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor,data=ads, family = "binomial")

OR <- function(x) exp(x)
stargazer(r1logit,r2logit,r3logit,r4logit, 
          apply.coef = OR, keep = c('gender','est_cost','demmention','repmention','won','number_candidates','multiple_winners','vote_per','incumbent','former_prosecutor'),title="Gender and Reform Campaign Appeals in 2017 and 2018 Judicial Elections, Unique Ads Only (Logit Odds Ratios)",
          notes="Results from logit regressions. Values represent odds ratios.",
          covariate.labels = c("Female","Logged Ad Cost","Democrat Mention","Republican Mention","Candidate Won","Number of Candidates","Multiple Winners","Vote Percentage","Incumbent","Former Prosecutor"),
          dep.var.labels = c("Reform","Compassionate","Victim","Second Chance"),label = "rehabappealslogit",
          out = "./tables/TableA12.tex",notes.align = "l",font.size="tiny",column.sep.width = "-10pt")

#######################################
# Table 13
#######################################
r1 <- felm(trait_tough~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|state,data=ads)
r2 <- felm(trait_fighter~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|state,data=ads)
r3 <- felm(trait_prosecutor~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|state,data=ads)
r4 <- felm(trait_policeendorse~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|state,data=ads)
r5 <- felm(trait_experienced~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|state,data=ads)
r6 <- felm(trait_protect~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|state,data=ads)
r7 <- felm(trait_strong~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|state,data=ads)

stargazer(r1,r2,r3,r4,r5,title="Gender and Tough-on-Crime Campaign Appeals in 2018 Judicial Elections, Unique Ads Only (State Clustered Standard Errors)",
          notes=c("Results from an OLS regression. Ad tone, program type, part of day, state,","affiliate, race category, and election type fixed effects included. SEs clustered by state."),
          covariate.labels = c("Female","Logged Ad Cost","Democrat Mention","Republican Mention","Candidate Won","Number of Candidates","Multiple Winners","Vote Percentage","Incumbent","Former Prosecutor"),
          dep.var.labels = c("Tough","Fight","Prosecutor","Police Endorse","Experienced","Protect","Strong"),label = "toughappealscluster",
          out = "./tables/TableA13.tex",notes.align = "l",font.size="tiny",column.sep.width = "-10pt")

#######################################
# Table 14
#######################################
r1 <- felm(trait_reform~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|state,data=ads)
r2 <- felm(trait_compassionate~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|state,data=ads)
r3 <- felm(trait_victim~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|state,data=ads)
r4 <- felm(trait_secondchance~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|state,data=ads)

stargazer(r1,r2,r3,r4,title="Gender and Reform Campaign Appeals in 2018 Judicial Elections, Unique Ads Only (State Clustered Standard Errors)",
          notes=c("Results from an OLS regression. Ad tone, program type, part of day, state,","affiliate, race category, and election type fixed effects included. SEs clustered by state."),
          covariate.labels = c("Female","Logged Ad Cost","Democrat Mention","Republican Mention","Candidate Won","Number of Candidates","Multiple Winners","Vote Percentage","Incumbent","Former Prosecutor"),
          dep.var.labels = c("Reform","Compassionate","Victim","Second Chance"),label = "rehabappealscluster",
          out = "./tables/TableA14.tex",notes.align = "l",font.size="tiny",column.sep.width = "-7pt") 

#######################################
# Table 15
#######################################
reg6 <- felm(toughtotal~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|0,data=ads)
reg7 <- felm(anytough~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|0,data=ads)
reg8 <- felm(reformtotal~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|0,data=ads)
reg9 <- felm(anyreform~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|0,data=ads)

stargazer(reg6,reg7,reg8,reg9,title="Gender and Reform and Tough-on-Crime Campaign Appeals in 2017 and 2018 Judicial Elections, Unique Ads Only",
          notes=c("Results from an OLS regression. Ad tone, program type, part of day, state,","affiliate, race category, and election type fixed effects included."),
          covariate.labels = c("Female","Logged Ad Cost","Democrat Mention","Republican Mention","Candidate Won","Number of Candidates","Multiple Winners","Vote Percentage","Incumbent","Former Prosecutor"),
          dep.var.labels = c("Tough Index (1-7)","Any Tough","Reform Index (1-4)","Any Reform"),label = "appealsindexany",
          out = "./tables/TableA15.tex",notes.align = "l",font.size="tiny")

#######################################
# Figure 1
#######################################
r1 <- felm(trait_tough~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|0,data=ads)

models <- list("Tough" = r1)

cm <- c('as.factor(gender)1' = 'female')
ci_90 <- modelplot(models, coef_omit = c(-1), conf_level = .90, coef_map = cm)+ theme_classic()+scale_color_grey()+
  #  gghighlight(p.value<0.1,unhighlighted_colour=alpha("lightgray", 0.3))+
  geom_vline(xintercept=0,linetype="dotted")+ggtitle("90% CIs")

ci_90 + geom_vline(xintercept = -sd(ads$trait_tough)/2, lty = 2)+ 
  geom_vline(xintercept = sd(ads$trait_tough)/2, lty = 2) +
  scale_x_continuous(breaks = c(-0.1, -sd(ads$trait_tough, na.rm = T)/2, 0,  sd(ads$trait_tough, na.rm = T)/2,  0.1),
                     labels = c("-0.1", "-0.122\n(-1/2 SD)", "0",   "0.122\n(1/2 SD)", "0.1"),
                     limits = c(-0.2, 0.2))

ggsave("./figures/FigureA1.pdf",height=8,width=12)

#######################################
# Table 16
#######################################
louisiana <- read.xlsx("./Louisiana_sample.xlsx") %>%
  mutate(`Candidate Type`=ifelse(Sample==1,"Ran TV Ads","Did Not Run TV Ads"))

st(louisiana, group = 'Candidate Type',
   vars=c("Party","Gender","Black","Vote.Share","Winner",
          "Court.Type","Prosecutor.Experience","Judge.Experience","Incumbent"),group.test=T,
   out="latex",file="./tables/TableA16.tex",
   title="Comparison of Louisiana judicial candidates in our sample (those who ran television ads) and those candidates that did not.",
   anchor="louisianacomparison")
